package com.csd.util;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;


public class HttpHelper {

	public static String HttpPost(String bf, String urlStr) {

		URL httpurl;
		String result = "";
		try {
			httpurl = new URL(urlStr);
			HttpURLConnection httpConn = (HttpURLConnection) httpurl
					.openConnection();
			httpConn.setDoInput(true);
			httpConn.setDoOutput(true);
			httpConn.setUseCaches(false);
			httpConn.setConnectTimeout(10000);// 请求时间设置6秒
			httpConn.setReadTimeout(10000);
			httpConn.setRequestMethod("POST");
			httpConn.setRequestProperty("Content-Type", "application/json");
			httpConn.setRequestProperty("Content-Length", ""
					+ bf.toString().getBytes("UTF-8").length);
			DataOutputStream out = new DataOutputStream(
					httpConn.getOutputStream());// 输入参数
			out.write(bf.toString().getBytes("UTF-8"));
			out.flush();
			out.close();// 写入参数*/
			int t = httpConn.getResponseCode();
			if (t == HttpURLConnection.HTTP_OK) {
				BufferedReader in = new BufferedReader(new InputStreamReader(
						httpConn.getInputStream(),"utf-8"));
				String line;
				while ((line = in.readLine()) != null) {
					result += line;
				}
				in.close();
				httpConn.disconnect();
				
			} else {

				return "";
			}
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		}
		return result;
	}
}
